/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots.testing;

import java.util.HashSet;
import java.util.LinkedList;

import lime2.Location;
import lime2.TupleID;
import sagroup.dracots.DracoTS;
import sagroup.dracots.DracoTuple;
import sagroup.dracots.Field;
import sagroup.dracots.ShareTuple;
import sagroup.dracots.SystemTupleType;
import sagroup.dracots.TupleType;
import sagroup.dracots.policy.RPTuple;

/**
 * @author Zheng Xiang
 *
 */
public class ListTuple extends ShareTuple {


	/**
	 * 
	 */
	private static final long serialVersionUID = -3439629948159769133L;
	
	@Field
	LinkedList<TupleID> tupleIDList 
	= new LinkedList<TupleID>();
	
	@Field
	TupleType fortype;
	
	@Field
	Location server;
	
	public ListTuple()
	{
		
	}
	
	
	
	/**
	 * 
	 */
	private ListTuple(HashSet<TupleID>  list,TupleType type) {
		super(new TupleType("List Tuple"), 	DracoTS.nextVID(), 
				0L, null,null, false);
		this.fortype = type;	
		this.server = Location.HERE;
		if(list == null)
			throw new IllegalArgumentException("list is null");
		
		for(TupleID tid :list)
		{
			if(!tupleIDList.contains(tid))
			   tupleIDList.add(tid);
		}
		
	}
	
	
	public static ListTuple createLocalList(TupleType type,HashSet<TupleID>  list)
	{
		ListTuple lt = new ListTuple(list,type);
		lt.setCurr(Location.HERE);
		lt.setDest(Location.HERE);
		lt.fortype = type;
		return lt;
	}

	public static ListTuple createListTemplate(TupleType type,Location here,Location remote)
	{
		ListTuple lt = (ListTuple)DracoTuple.createTemplateByClass(ListTuple.class);
		lt.fortype = type;
		lt.setMatchAll(true);
		lt.setCurr(here);
		lt.setDest(here);
		lt.setServer(remote);
		return lt;
	}
	
	
	private void setServer(Location remote) {
		this.server = remote;
		
	}



	public LinkedList<TupleID> getTupleList()
	{
		return tupleIDList;
	}



	public static ListTuple createListTemplate(TupleType type, Location location) {
		ListTuple lt = (ListTuple)DracoTuple.createTemplateByClass(ListTuple.class);
		lt.fortype = type;
		lt.setMatchAll(true);
		lt.setCurr(location);
		lt.setDest(location);
		
		return lt;
	}

}
